"""
Phase 3: Exchange Rates
========================
Z → REER: aging → surplus → real appreciation?
"""

import sys
from pathlib import Path

import numpy as np
import pandas as pd

PROJECT_DIR = Path("/mnt/c/demographics_capital_flows/asset_returns")
MULTILATERAL_DIR = PROJECT_DIR.parent / "multilateral"
sys.path.insert(0, str(MULTILATERAL_DIR / "src"))
from model import PanelGLS

PROCESSED_DIR = PROJECT_DIR / "data" / "processed"
TABLES_DIR = PROJECT_DIR / "output" / "tables"

OECD_38 = [
    "AUS", "AUT", "BEL", "CAN", "CHL", "COL", "CRI", "CZE", "DNK", "EST",
    "FIN", "FRA", "DEU", "GRC", "HUN", "ISL", "IRL", "ISR", "ITA", "JPN",
    "KOR", "LVA", "LTU", "LUX", "MEX", "NLD", "NZL", "NOR", "POL", "PRT",
    "SVK", "SVN", "ESP", "SWE", "CHE", "TUR", "GBR", "USA",
]

CONTROLS = ['rgdp_growth', 'inflation', 'fiscal_bal_gdp', 'kaopen', 'nfa_gdp_lag']


def stars(p):
    if p < 0.01: return '***'
    if p < 0.05: return '**'
    if p < 0.10: return '*'
    return ''


def run_model(df, dep_var, regressors, label):
    """Run PanelGLS and return results dict."""
    regressors = [r for r in regressors if r in df.columns]
    if dep_var not in df.columns:
        print(f"  [{label}] {dep_var} missing — skipping")
        return None

    sub = df.dropna(subset=[dep_var] + regressors).copy()
    if len(sub) < 50:
        print(f"  [{label}] Insufficient obs ({len(sub)}) — skipping")
        return None

    gls = PanelGLS()
    gls.fit(sub[dep_var].values, sub[regressors].values,
            sub['iso3'].values, sub['year'].values)

    print(f"\n  [{label}]  N={gls.n_obs}, countries={gls.n_countries}, "
          f"R²={gls.r_squared:.4f}")

    results = {
        'label': label, 'dep_var': dep_var,
        'n_obs': gls.n_obs, 'n_countries': gls.n_countries,
        'r_squared': gls.r_squared, 'rho': gls.rho,
    }
    for i, name in enumerate(regressors):
        results[f'coef_{name}'] = gls.beta[i]
        results[f'se_{name}'] = gls.se[i]
        results[f'p_{name}'] = gls.pvalues[i]
        sig = stars(gls.pvalues[i])
        print(f"    {name:<25} {gls.beta[i]:>8.4f} ({gls.se[i]:.4f}) {sig}")

    return results


def main():
    print("=" * 70)
    print("PHASE 3: Exchange Rates")
    print("=" * 70)

    df = pd.read_csv(PROCESSED_DIR / "asset_panel.csv")
    print(f"Panel: {df['iso3'].nunique()} countries, {len(df)} obs")

    all_results = []
    demo_vars = ['Z_1', 'Z_2', 'Z_3']
    controls = [c for c in CONTROLS if c in df.columns]

    # ── Model 1: log_reer = Z + controls (level) ──
    r = run_model(df, 'log_reer', demo_vars + controls, "M1: Z → log REER")
    if r: all_results.append(r)

    # ── Model 2: d_reer = Z + controls (change) ──
    r = run_model(df, 'd_reer', demo_vars + controls, "M2: Z → ΔREER")
    if r: all_results.append(r)

    # ── Model 3: Z × KAOPEN interaction ──
    int_vars = [v for v in ['Z_1_x_kaopen', 'Z_2_x_kaopen', 'Z_3_x_kaopen']
                if v in df.columns]
    if int_vars:
        r = run_model(df, 'log_reer', demo_vars + controls + int_vars,
                      "M3: Z×KAOPEN → log REER")
        if r: all_results.append(r)

    # ── Model 4: Z × NFA interaction ──
    if 'nfa_gdp_lag' in df.columns:
        for zv in demo_vars:
            df[f'{zv}_x_nfa'] = df[zv] * df['nfa_gdp_lag']
        nfa_int = [f'{zv}_x_nfa' for zv in demo_vars]
        r = run_model(df, 'log_reer', demo_vars + controls + nfa_int,
                      "M4: Z×NFA → log REER")
        if r: all_results.append(r)

    # ── Model 5: old_dep + youth_dep ──
    age_vars = ['old_dep', 'youth_dep']
    r = run_model(df, 'log_reer', age_vars + controls, "M5: age ratios → log REER")
    if r: all_results.append(r)

    # ── Model 6: OECD vs non-OECD ──
    oecd = df[df['iso3'].isin(OECD_38)].copy()
    non_oecd = df[~df['iso3'].isin(OECD_38)].copy()
    r = run_model(oecd, 'log_reer', demo_vars + controls, "M6a: OECD Z → log REER")
    if r: all_results.append(r)
    r = run_model(non_oecd, 'log_reer', demo_vars + controls, "M6b: non-OECD Z → log REER")
    if r: all_results.append(r)

    # ── Model 7: carry_vs_usa = Z + controls ──
    r = run_model(df, 'carry_vs_usa', demo_vars + controls, "M7: Z → carry vs USA")
    if r: all_results.append(r)

    # ── Build table ──
    build_table(all_results)

    print("\n" + "=" * 70)
    print("Phase 3 complete.")
    print("=" * 70)


def build_table(all_results):
    if not all_results:
        return

    key_vars = ['Z_1', 'Z_2', 'Z_3', 'old_dep', 'youth_dep',
                'Z_1_x_kaopen', 'Z_2_x_kaopen', 'Z_3_x_kaopen',
                'Z_1_x_nfa', 'Z_2_x_nfa', 'Z_3_x_nfa']

    md = ["# Exchange Rate Results\n"]
    md.append("| Model | Dep Var | N | Countries | R² |")
    md.append("|---|---|---|---|---|")
    for r in all_results:
        md.append(f"| {r['label']} | {r['dep_var']} | {r['n_obs']} "
                  f"| {r['n_countries']} | {r['r_squared']:.3f} |")

    md.append("\n## Key Coefficients\n")
    md.append("| Model | Variable | Coef | SE | p-value | Sig |")
    md.append("|---|---|---|---|---|---|")
    for r in all_results:
        for var in key_vars:
            ckey = f'coef_{var}'
            if ckey in r:
                p = r[f'p_{var}']
                md.append(f"| {r['label']} | {var} | {r[ckey]:.4f} "
                          f"| {r[f'se_{var}']:.4f} | {p:.4f} | {stars(p)} |")

    md.append(f"\n*Controls: {', '.join(CONTROLS)}*")

    out = TABLES_DIR / "exchange_rates.md"
    out.write_text('\n'.join(md))
    print(f"  Saved: {out}")


if __name__ == "__main__":
    main()
